package com.main;

import static com.util.Constant.*;

import com.util.GameUtil;

import java.awt.*;
import java.awt.image.BufferedImage;

public class Bird {
    //    小鸟矩形对象
    private Rectangle rect;
    //    小鸟的生命值
    public boolean life = true;
    //    存放小鸟图片
    private BufferedImage[] images;
    public static final int BIRD_IMG_COUNT = 3;

    //    小鸟加速度
    private int acceleration;

    //    定义鸟的状态
    private int state;
    public static final int STATE_NORMAR = 0;  //横着飞
    public static final int STATE_UP = 1;      //向上飞
    public static final int STATE_DOWN = 2;    //向下飞

    //    鸟的位置
    private int x = 200, y = 200;

    //    鸟上下移动
    private boolean up = false, down = false;

    //    鸟的移动速度
    private int speed = 4;

    //    构造方法中初始化资源
    public Bird() {
        images = new BufferedImage[BIRD_IMG_COUNT];
        for (int i = 0; i < BIRD_IMG_COUNT; i++) {
            images[i] = GameUtil.loadBufferedImage(BIRD_IMG[i]);
        }
        int w = images[0].getWidth();
        int h = images[0].getHeight();
        rect = new Rectangle(w, h);
    }

    //    绘制小鸟
    public void draw(Graphics g) {
        flylogic();
        g.drawImage(images[state], x, y, null);

//        绘制小鸟的矩形
//        g.drawRect(x, y, (int) rect.getWidth(), rect.height);
        rect.x = this.x;
        rect.y = this.y;
    }

    //    控制鸟的方向
    public void flylogic() {
        if (up) {
            acceleration--;
            y += acceleration;
            if (acceleration < -10) {
                acceleration = -10;
            }
            if (y < 20) {
                y = 20;
                acceleration = 0;
            }
        }
        if (!up) {
            acceleration++;
            y += acceleration;
            if (acceleration > 10) {
                acceleration = 10;
            }
            if (y > 445) {
                y = 445;
                acceleration = 0;
            }
        }
    }

    //    控制鸟的移动方向
    public void fly(int fly) {
        switch (fly) {
            case 1:
                state = 1;
                up = true;
                break;
            case 5:
                state = 2;
                up = false;
                break;
        }
    }

    public Rectangle getRect() {
        return rect;
    }

    /*
     * 重新绘制小鸟的位置
     */
    public void restartDraw() {
        life = true;
        x = 200;
        y = 200;
    }
}
